<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <style>
        body{
            background-image: url("./image/background_Login.jpg");
            background-size: 100%;
        }
    </style>
</head>
<body>
    <!--    导航栏-->
    <div class="nav">
        <img src="image/person_Blog.png" alt="">
        <span>我的博客系统</span>
        <div class="spacer"></div>
        <a href="main.html">主页</a>
        <a href="edit.html">写博客</a>
        <a href="enroll.html">注册</a>
    </div>
    <div class="login-container">
        <div class="login-dialog">
            <h3>登录</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username" onkeyup="this.value=this.value.replace(/[, ]/g,'')">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <input type="submit" id="submit">
            </div>
        </div>
    </div>

    <script src="jquery.js"></script>
    <script>
        submitMethod = function(){
            let username = document.querySelector("#username");
            let password = document.querySelector("#password");
            let user = {
                username: username.value,
                password: password.value
            }
            $.ajax({
                type: 'post',
                url: 'login',
                contentType: 'application/json',
                data: JSON.stringify(user),
                success: function (body) {
                    if (body == 'usernameNull'){
                        alert('用户名不能为空!')
                    }else if (body == 'passwordNull'){
                        alert('密码不能为空!')
                    }else if (body == 'failed'){
                        alert('用户名或密码错误!')
                    }else if (body == 'success'){
                        location = 'main.html'
                    }
                    // 清空内容
                    username.value = '';
                    password.value = '';
                }
            })
        }
        let submit = document.querySelector("#submit");
        submit.onclick = function () {
            submitMethod();
        }
        $('#username').bind('keydown', function (e) {
            // enter键
            if(e.keyCode == 13){
                submitMethod();
            }
        })
        $('#password').bind('keydown', function (e) {
            // enter键
            if(e.keyCode == 13){
                submitMethod();
            }
        })
    </script>
</body>
</html>